package team.bailan.controller.user;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import team.bailan.common.config.ReturnResponse;
import team.bailan.common.config.WechatConfig;
import team.bailan.common.util.MD5;
import team.bailan.entity.domain.User;
import team.bailan.entity.dto.UserDTO;
import team.bailan.mapper.UserMapper;
import team.bailan.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("api/user")
public class UserController {
    private final RestTemplate restTemplate = new RestTemplate();
    @Autowired
    public WechatConfig wechatConfig;
    @Autowired
    public UserService userService;
    /*@PostMapping("login")
    public String login(HttpServletRequest request, @RequestBody HashMap<String, String> resultMap) {
        System.out.println(request.getHeader("test"));
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid={appid}&secret={secret}&js_code={code}&grant_type=authorization_code";
        Map<String, String> requestMap = new HashMap<>();
        requestMap.put("appid", wechatConfig.getAppid());
        requestMap.put("secret", wechatConfig.getSecret());
        requestMap.put("code", resultMap.get("code"));
        String result = this.restTemplate.getForObject(url, String.class,requestMap);
        System.out.println(result);
        HashMap map = JSON.parseObject(result, HashMap.class);
        String openId = map.get("openid").toString();
        String token = MD5.getInstance().getMD5(map.get("session_key").toString());
        User user = new User();
        user.setOpenId(openId);
        user.setToken(token);
        if (userMapper.selectCount(new QueryWrapper<User>().eq("open_id", openId)) == 0) {
            userMapper.insert(user);
        } else {
            userMapper.updateById(user);
        }
//        System.out.println(token);
        return token;

    }*/

    @ApiOperation("用户注册")
    @PostMapping("register")
    public ReturnResponse<String> register(@RequestBody UserDTO userDTO) {
        return userService.register(userDTO);
    }

    @ApiOperation("用户登录")
    @PostMapping("login")
    public ReturnResponse<String> login(HttpServletRequest request, HttpServletResponse response, @RequestBody UserDTO userDTO) {
        return userService.login(request, response, userDTO);
    }

    public void updateBeans(String[] usernameList, Integer[] userBeans) {
        userService.updateBeans(usernameList, userBeans);
    }
}
